package com.example.synatx.stream;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

import static java.util.stream.Collectors.*;

/**
 * @auth guoxh
 * @date 2020/3/6 11:04
 * @desc
 */
public class TraderTest {
    Trader Raoul = new Trader("Raoul","cambridge");
    Trader Mario = new Trader("Mario","milan");
    Trader Alan = new Trader("Alan","cambridge");
    Trader Brian = new Trader("Brian","cambridge");

    List<Transcation> transcations = Arrays.asList(
            new Transcation(Brian,2011,300),
            new Transcation(Raoul,2012,1000),
            new Transcation(Raoul,2011,400),
            new Transcation(Mario,2012,710),
            new Transcation(Mario,2011,700),
            new Transcation(Alan,2012,950)
    );

    /**
     * 2011年所有交易并按交易额排序
     */
    public void test01(){
        List<Transcation> list = transcations.stream().filter(t -> t.getYear()==2011).sorted(Comparator.comparing(Transcation::getValue)).collect(toList());
    }

    /**
     * 交易员在哪些城市呆过
     */
    public void test02(){
        List<String> list = transcations.stream().map(t -> t.getTrader().getCity()).distinct().collect(toList());
        Set<String> set = transcations.stream().map(t -> t.getTrader().getCity()).collect(toSet());

    }
    /**
     * 查找所有来自剑桥的交易员,并按姓名排序
     */
    public void test03(){
        List<Trader> traders = transcations.stream().filter(t ->t.getTrader().getCity().equals("cambridge")).map(t -> t.getTrader()).distinct().sorted(Comparator.comparing(Trader::getName)).collect(toList());
    }

    /**
     * 返回所有交易员的姓名字符串,按字母顺序排序
     */
    public void test04(){
        String traderStr = transcations.stream().map(t -> t.getTrader().getName()).distinct().sorted()
                .reduce("",(n1,n2) -> n1 + n2);

        String str = transcations.stream().map(t -> t.getTrader().getName()).distinct().sorted()
                .collect(joining());
    }

    /**
     * 有没有交易员在米兰工作的
     */
    public void  test05(){
        boolean flag = transcations.stream().anyMatch(t -> t.getTrader().getCity().equals("milan"));
    }

    /**
     * 打印生活在剑桥的交易员的所有交易额
     */
    public void test06(){
        transcations.stream().filter(transcation -> transcation.getTrader().getCity().equals("cambridge"))
                .map(transcation -> transcation.getValue())
                .forEach(System.out::println);
    }

    /**
     * 所有交易中最高的交易额
     */
    public void test07(){
        transcations.stream().map(Transcation::getValue).reduce(Integer::max);
    }

    /**
     * 所有交易额最小的交易
     */
    public void test08(){
        transcations.stream().reduce((t1,t2) -> t1.getValue()>t2.getValue()?t2:t1);

        transcations.stream().min(Comparator.comparing(Transcation::getValue));
    }
}
